/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.admin.client.components;

import java.util.ArrayList;
import java.util.List;

import com.elf.gwt.admin.client.AdminComponentController;
import com.elf.gwt.common.client.model.CustomerReportModel;
import com.elf.gwt.common.client.utils.CommonUtils;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;

/**
 *  
 */
public class CustomerReportComponent extends EditTableComponent
{	
	protected List<CustomerReportModel> customerReportModels;

	/**
	 * Constructor
	 */
	public CustomerReportComponent(AdminComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix);
		editable = false;
		customerReportModels = new ArrayList<CustomerReportModel>();
		paginate = true;
		tableType = "customers";
	}

	protected void buildMetaModel(JSONObject jsonObject) throws Exception
	{
		super.buildMetaModel(jsonObject);
	}

	protected void buildModel(JSONArray jsonArray) throws Exception
	{
		customerReportModels.clear();
		super.buildModel(jsonArray);
	}

	protected void buildModel(JSONObject jsonObject) throws Exception
	{
		CustomerReportModel model = new CustomerReportModel(jsonObject);
		customerReportModels.add(model);
	}

	public void buildFromModel() throws Exception
	{
		numberOfTableEntries = customerReportModels.size();
		numberOfTableColumns = 4;
		super.buildFromModel();
	}

	public void displayHeader(Grid grid) {
		grid.setText(0, 0, "Customer");
		grid.setText(0, 1, "Company");
		grid.setText(0, 2, "Status");
		grid.setText(0, 3, "Total");
	}

	public void displayRow(Grid grid, int row, int entryIndex) {
		displayRow(grid, row, customerReportModels.get(entryIndex));
	}

	public void displayRow(Grid grid, int row, CustomerReportModel model) {
		CellFormatter cf = grid.getCellFormatter();
		grid.setText(row, 0, model.getName());
		grid.setText(row, 1, model.getCompany());
		grid.setText(row, 2, model.getStatus());
		cf.setHorizontalAlignment(row, 3, HasHorizontalAlignment.ALIGN_RIGHT);
		grid.setHTML(row, 3, CommonUtils.priceDisplay(AdminComponentController.currenciesAdministrationComponent.getCurrencyModel(), model.getTotal()));	
	}

	public List<CustomerReportModel> getCustomerReportModels() {
		return customerReportModels;
	}
}
